define(function (require, exports, module) {
  'use strict';
  var $ = require('jquery'),
      _ = require('underscore'),
      Backbone = require('backbone'),
      notify = require('js/utils/notify'),
      models = require('js/apps/op/models'),
      utils = require('js/utils/tools'),
      tz = require('js/utils/tz'),
      moment = require('moment'),
      app = Backbone.history;
  require('datetimepicker');
  var MetisSixListView = Backbone.View.extend({
      tagName: "div",
      events: {
          'click #search': 'doSearch',
      },
      initialize: function (options) {
          this.options = options;
          this.collection = new models.MetisSixCollection();
          this.collection.bind('change reset remove', this.renderWithData, this);
      },
      render: function () {
          this.$el.html("");
          $(window).scrollTop(0);
          return this;
      },
      doSearch: function () {
          var options = {},
              searched = {};
          searched.create_time_lower = $("#searchCreatedAtLower").val();
          searched.create_time_upper = $('#searchCreatedAtUpper').val();
          if (searched.create_time_lower || searched.create_time_upper) {
              options.create_time = {};
              if (searched.create_time_lower) options.create_time.$gte = searched.create_time_lower;
              if (searched.create_time_upper) options.create_time.$lt = searched.create_time_upper;
          }
          utils.saveSearched('op/metis_six', searched);
          app.navigate(utils.composeQueryString('#op/metis_six/', options), {
              trigger: true
          });
      },
      renderWithData: function () {
          this.$el.html(this.template({
              items: this.collection.toJSON(),
              searched: utils.getSearched('op/metis_six')
          }));
          var that = this;
          utils.renderTable('main-list', {
              $orderby: that.options.$orderby || 'id',
              sortCallback: function (field) {
                  that.options.$orderby = field;
                  that.options.$page = 1;
                  var newUrl = utils.composeQueryString('#op/metis_six/', that.options);
                  app.navigate(newUrl, {
                      trigger: true
                  });
              }
          });
          $('[data-type=date]').datetimepicker({
              defaultDate: false,
              format: 'YYYY-MM-DD HH:mm:ss'
          });
          utils.getPaginator(that.options, this.collection.total, '#op/metis_six/');

          return this;
      },
      load: function () {
          this.options.game_id = 5502
          this.collection.fetch({
              reset: true,
              data: this.options,
              error: function (c, r, o) {
                  notify.notifyResp(r);
              },
          });
      }
  });
  exports.List = Backbone.View.extend({
      el: "#content_wrapper",
      template: "op/MetisSixList.html",

      initialize: function (options) {
          this.options = options;
      },
      render: function () {
          if (!this.options.create_time) this.options.create_time = JSON.stringify({"$gte": tz.today()+' 00:00:00'});
          var view = new MetisSixListView(this.options);
          view.template = this.template;

          this.$el.empty();
          this.$el.append(view.render().el);
          view.load();

          return this;
      }
  });

});
